package com.example.nacos.controller;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.alibaba.fastjson.JSON;
import com.example.nacos.comp.BatchClickhouseComp;
import com.example.nacos.entity.DlsFundNavInfo;
import com.example.nacos.utils.FileReaderUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
public class FileBatchController {

    @Autowired
    private DruidDataSource dataSource;

    @Autowired
    private BatchClickhouseComp batchClickhouseComp;

    @GetMapping("/file")
    public String file() throws IOException, SQLException, NoSuchFieldException, IllegalAccessException {
        List<DlsFundNavInfo> list = FileReaderUtil.readTextFile("C:\\Users\\41302\\Desktop\\pack\\DLS_FUND_NAV_INFO.dat", DlsFundNavInfo.class);
        log.info("clickhouse批量插入开始 {}", JSON.toJSONString(list));
        batchClickhouseComp.health(list,DlsFundNavInfo.class);
        return "ok";

    }


    /**
     * 大零售-基金产品、资管计划产品净值信息表 文件名称 DLS_FUND_NAV_INFO.dat
     * @return
     */
    @GetMapping("/sql")
    public String health() throws IOException, SQLException {
        // 下载文件到服务器 打开文件并且将文件内容转换为List<bean>
//        List<DlsFundNavInfo> list = FileReaderUtil.readTextFile("C:\\Users\\41302\\Desktop\\pack\\DLS_FUND_NAV_INFO.dat", DlsFundNavInfo.class);
        DlsFundNavInfo dlsFundNavInfo= new DlsFundNavInfo();
        dlsFundNavInfo.setProdCode("1");
        dlsFundNavInfo.setProdName("1");
        dlsFundNavInfo.setNetDate("1");
        dlsFundNavInfo.setNavDate("1");
        dlsFundNavInfo.setNav(1.0);
        dlsFundNavInfo.setTotalNav(1.0);
        dlsFundNavInfo.setProdStatus("1");
        dlsFundNavInfo.setBusiType("1");
        dlsFundNavInfo.setDataSource("1");

        dlsFundNavInfo.setLoadDate(LocalDate.now());
        dlsFundNavInfo.setTotalFundVol(1.0);
        dlsFundNavInfo.setFundYearIncomeRate(1.0);
        dlsFundNavInfo.setYield(1.0);
        dlsFundNavInfo.setFundIncome(1.0);
        List<DlsFundNavInfo> list = new ArrayList<>();
        list.add(dlsFundNavInfo);

        DruidPooledConnection pooledConnection = dataSource.getConnection();
        log.info("clickhouse批量插入开始");
        PreparedStatement pstmt = pooledConnection
                .prepareStatement("INSERT INTO etl_test  ( id, name )  VALUES (  ?, ?  )");
        // clickhouse 最多批量插入数据 447条记录
        for (int i = 0; i < 447; i++) {
            String id = "10" + i;
            String sku = "sku20231108" + i;
            pstmt.setString(1, id);
            pstmt.setString(2, sku);

            pstmt.addBatch();
        }
        pstmt.executeBatch();
        log.info("clickhouse批量插入成功");
        return "ok";
    }

}